package service;

import java.util.ArrayList;
import java.util.List;

import modelo.*;
import beans.*;
import entities.*;

public class FacturaSrv {

	private static FacturaSrv instancia = null;
	
	public static FacturaSrv getInstancia()
	{
		if(instancia == null) {
			instancia = new FacturaSrv();
		}
		return instancia;
	}
	
	public void guardarFactura(FacturaBean facturaBean) {
		FacturaEntity facturaEntity = this.bean2Entity(facturaBean);
		FacturaDao.getInstancia().merge(facturaEntity);
		
	}

	public FacturaEntity bean2Entity(FacturaBean facturaBean) {
		FacturaEntity entity = new FacturaEntity();
		entity.setNroFactura(facturaBean.getNroFactura());
		List<ItemCartaEntity> itemsCartaEntity = new ArrayList<ItemCartaEntity>();
		for (ItemCartaBean itemCartaBean: facturaBean.getItemsCarta()) {
			ItemCartaEntity itemCartaEntity = ItemCartaSrv.getInstancia().bean2Entity(itemCartaBean);
			itemsCartaEntity.add(itemCartaEntity);
		}
		entity.setItemsFactura(itemsCartaEntity);
		return entity;
	}
	
	
	public FacturaBean entity2Bean(FacturaEntity facturaEntity) {
		FacturaBean bean = new FacturaBean();
		List<ItemCartaBean> itemsCartaBean = new ArrayList<ItemCartaBean>();
		for (ItemCartaEntity itemCartaEntity: facturaEntity.getItemsFactura()) {
			ItemCartaBean itemCartaBean = ItemCartaSrv.getInstancia().entity2Bean(itemCartaEntity);
			itemsCartaBean.add(itemCartaBean);
		}
		bean.setItemsCarta(itemsCartaBean);
		return bean;
	}
	
	
}
